package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.*;
import com.qf.mapper.AnnounceDao;
import com.qf.mapper.LogsDao;
import com.qf.service.LogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/12
 * qjh
 * springboot_oa_five
 * 面向对象面向君 不负代码不负卿
 */
@Service
public class LogsServiceImpl implements LogsService {


    @Autowired
    private LogsDao logsDao;

    @Override
    public PageInfo<Logs> findAllLogs(Integer page, String userName, String operTimeBegin, String operTimeOver) {
        PageHelper.startPage(page, 5);
        Map map = new HashMap();
        map.put("userName",userName);
        map.put("operTimeBegin",operTimeBegin);
        map.put("operTimeOver",operTimeOver);
        List<Logs> allLogs = logsDao.findAllLogs(map);
        PageInfo<Logs> pageInfo = new PageInfo<>(allLogs);
        return pageInfo;
    }

    @Override
    public List<Logs> excelLogs(int[] arr) {
        List<Logs> logs = logsDao.excelLogs(arr);
        return logs;
    }

    @Override
    public void addAnnunciate(User user, Annunciate annunciate) {
        Logs logs = new Logs();
        String username = user.getUsername();
        Role roles = user.getRoles();
        String roleName = roles.getRoleName();
        logs.setRoleName(roleName);
        logs.setUserName(username);
        logs.setOperTime(annunciate.getPublishTime());
        logs.setLogWork("发布了通告");
        logs.setLogStatus(annunciate.getStatus());
        logs.setLogContent(annunciate.getAnnName());
        logsDao.addLogs(logs);
    }

    @Override
    public void addUser(User user) {
        Logs logs = new Logs();
        String username = user.getUsername();
        Role roles = user.getRoles();
        String roleName = roles.getRoleName();
        logs.setRoleName(roleName);
        logs.setUserName(username);
        logs.setLogWork("添加了用户");
        logs.setLogStatus(1);
        logs.setLogContent("无");
        Date date = new Date();
        logs.setOperTime(date);
        logsDao.addLogs(logs);
    }

    @Override
    public void addRole(User user, Role role) {
        Logs logs = new Logs();
        String username = user.getUsername();
        String roleName = role.getRoleName();
        String roleDesc = role.getRoleDesc();
        Integer roleStatus = role.getRoleStatus();
        logs.setRoleName(roleName);
        logs.setUserName(username);
        logs.setLogWork("添加了角色");
        Date date = new Date();
        logs.setOperTime(date);
        logs.setLogContent(roleDesc);
        logs.setLogStatus(roleStatus);
        logsDao.addLogs(logs);
    }

    @Override
    public void addMenu(User user, Menu menu) {
        Logs logs = new Logs();
        Integer menuStatus = menu.getMenuStatus();
        String username = user.getUsername();
        Role roles = user.getRoles();
        String roleName = roles.getRoleName();
        logs.setRoleName(roleName);
        logs.setUserName(username);
        logs.setLogWork("添加了菜单");
        logs.setLogStatus(menuStatus);
        logs.setLogContent("无");
        Date date = new Date();
        logs.setOperTime(date);
    }
}
